//
//  SelledViewController.m
//  Xspcf
//
//  Created by 赵欢 on 2018/1/18.
//  Copyright © 2018年 小小算盘. All rights reserved.
//

#import "SelledViewController.h"
#import "MJRefresh.h"
#import "SunSecondCell.h"
#import "SunProductDetailVC.h"
#import "ColorButton.h"

@interface SelledViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIButton *overButton;
@property (nonatomic, strong) UIButton *repayButtton;
@property (nonatomic, strong) UITableView *tb;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UILabel *redLine;


@end

@implementation SelledViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    [self requestBorrowList];
    self.view.backgroundColor = BACKGROUND;
    self.dataArr = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
}

- (void)createView {
    UIView *headImage = [[UIView alloc]init];
    headImage.backgroundColor = WHITE;
    [headImage setFrame:CGRectMake(0, -10, SCREENWIDTH, 55)];
    [self.view addSubview:headImage];
    headImage.userInteractionEnabled = YES;
    
    self.overButton = [[UIButton alloc]initWithFrame:CGRectMake(X(20), 13, 170, 40)];
    self.overButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
    [self.overButton setTitle:[NSString stringWithFormat:@"已售罄"] forState:UIControlStateNormal];
    self.overButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.overButton setTitleColor:COLOR_TITLE_MAIN forState:UIControlStateNormal];
    [self.overButton setTitleColor:COLOR_MAIN forState:UIControlStateSelected];
    [headImage addSubview:self.overButton];
    [self.overButton addTarget:self action:@selector(overAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.repayButtton = [[UIButton alloc]initWithFrame:CGRectMake(X(213), 13, 170, 40)];
    self.repayButtton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
    [self.repayButtton setTitle:[NSString stringWithFormat:@"已还款"] forState:UIControlStateNormal];
    self.repayButtton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.repayButtton setTitleColor:COLOR_TITLE_MAIN forState:UIControlStateNormal];
    [self.repayButtton setTitleColor:COLOR_MAIN forState:UIControlStateSelected];
    [headImage addSubview:self.repayButtton];
    [self.repayButtton addTarget:self action:@selector(repayAction) forControlEvents:UIControlEventTouchUpInside];
    self.redLine = [[UILabel alloc]initWithFrame:CGRectMake(X(76), 44, 45, 2)];
    self.redLine.backgroundColor = COLOR_MAIN;
    [self.view addSubview:self.redLine];
    if ([self.type isEqualToString:@"2"]) {
        self.overButton.selected = YES;
    }else {
        self.repayButtton.selected = YES;
        [self.redLine setFrame:CGRectMake(X(268), 44, 45, 2)];
    }
    self.tb = [[UITableView alloc]initWithFrame:CGRectMake(0, 55, SCREENWIDTH, SCREENHEIGHT - 45) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tb];
    self.tb.backgroundColor = [UIColor clearColor];
    self.tb.delegate = self;
    self.tb.dataSource = self;
    self.tb.tableFooterView = [[UIView alloc]init];
    self.tb.showsVerticalScrollIndicator = NO;
    self.tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestToRefresh)];
    self.tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestToMore)];
}
#pragma mark request
- (void)requestToRefresh{
    self.page = 1;
    //    [self requestToVirtual];
    [self requestBorrowList];
}

- (void)requestToMore{
    self.page ++;
    [_tb.mj_header endRefreshing];
    [_tb.mj_footer endRefreshing];
    [self requestBorrowList];
}
- (void)requestBorrowList{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWLIST];
    NSDictionary *param = @{@"page":[NSString stringWithFormat:@"%d",_page],@"status":self.type};
    [HttpRequest postURL:url withParam:param success:^(id requestDic, NSString *msg) {
//        [_sun hideActivity];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
        if (_page ==1) {
            [_dataArr removeAllObjects];
        }
        NSArray *arr = [requestDic objectForKey:@"list"];
        if (arr.count <= 0){
            [_tb.mj_footer endRefreshingWithNoMoreData];
        }else{
            [_tb.mj_footer resetNoMoreData];
        }
        [_dataArr addObjectsFromArray:arr];
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
//        [_sun hideActivity];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
        //        [[Sun shareSun]showMsg:errorInfo];
    }];
    
}

- (void)overAction{
    self.title = @"已售罄";
    [self.redLine setFrame:CGRectMake(X(76), 44, 45, 2)];
    self.overButton.selected = YES;
    self.repayButtton.selected = NO;
    self.type = @"2";
    self.page = 1;
    [self requestBorrowList];

}
- (void)repayAction{
    self.title = @"已还款";
    [self.redLine setFrame:CGRectMake(X(268), 44, 45, 2)];
    self.overButton.selected = NO;
    self.repayButtton.selected = YES;
    self.type = @"3";
    self.page = 1;
    [self requestBorrowList];
}

#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SunSecondCell *cell = [SunSecondCell cellWithTableView:tableView];
    NSDictionary *dic = _dataArr[indexPath.section];
    [cell setCellData:dic];
    if ([self.type isEqualToString:@"2"]) {
        cell.selledLabel.text = @"已售罄";
    }else {
        cell.selledLabel.text = @"已还款";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SunProductDetailVC *vc = [[SunProductDetailVC alloc]init];
    vc.borrowId = [_dataArr[indexPath.section] objectForKey:@"borrowId"];
    if ([[_dataArr[indexPath.section] objectForKey:@"desc"] length] > 0) {
        vc.title = [NSString stringWithFormat:@"%@ %@(%@)",[_dataArr[indexPath.section] objectForKey:@"name"],[_dataArr[indexPath.section] objectForKey:@"number"],[_dataArr[indexPath.section] objectForKey:@"desc"]];
    }else {
        vc.title = [NSString stringWithFormat:@"%@ %@",[_dataArr[indexPath.section] objectForKey:@"name"],[_dataArr[indexPath.section] objectForKey:@"number"]];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BACKGROUND;
    return view;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
